/*
 * @Author: your name
 * @Date: 2020-10-26 10:46:26
 * @LastEditTime: 2020-10-29 15:31:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /Project/node/services/OrderService.js
 */
const {querySql, queryOne, guard} = require('../utils')

/**下单接口 */
function createOrder(req,res,next) {
    /**
     * status
     * 0:  已下单  待支付
     * 1:  已支付  待接单
     * 2:  已接单  待领取
     * 3:  已领取  待配送
     * 4:  已完成  -----
     */

    guard(req,next, () => {
        //建表Orders
        const orderSql = `create table if not exists Orders (
            orderId INT NOT NULL AUTO_INCREMENT COMMENT '订单id',  
            status INT NOT NULL DEFAULT 0 COMMENT '订单状态',
            userName VARCHAR(11) NOT NULL COMMENT '订单用户名称', 
            userPhone VARCHAR(255) NOT NULL COMMENT '订单手机号', 
            orderPrice INT NOT NULL COMMENT '订单总金额', 
            createTime DATETIME(3) NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '订单插入时间，也就是下单时间', 
            PRIMARY KEY (orderId) )`
            
        querySql(orderSql).then((response) => {
            
            if (response) {
                //插入一条新的订单数据
                let { userName, userPhone,orderPrice } = req.body; 
                let insertSql = `INSERT INTO myDB.Orders (userName, userPhone,orderPrice) VALUES ("${userName}", "${userPhone}",${orderPrice})`
                querySql(insertSql).then((result) =>{
                    res.json({code:200,data:{orderId:result.insertId},msg:'下单成功'})
                    //插入成功以后 查询这条的订单id返回过去
                    
                })
            }
            }).catch((err) => {
                res.json({code:300,data:null,msg:'生成失败'})
            })

    })
}


/**获取订单列表 */
function OrderInfo(req,res,next) {
    
    guard(req,next, () => {
        
        let {status} = req.body;
        let sql = `SELECT * FROM myDB.Orders`
        if (status != 0)  sql += ` WHERE status=${status}`
        querySql(sql).then((result) =>{
            res.json({code:200, data:result,msg:'操作成功'})
        })
    })
}

/**模拟订单支付成功 */
function OrderPayStatus(req,res,next) {
    guard(req,next, () => {
        let {orderId} = req.body;
        updateOrderStatus(orderId,1).then((result) => {
            res.json({code:200, data:result,msg:'支付成功'})
        })
    })
}

//商家确认接单
function confirmOrder(req, res, next) {
    guard(req,next, () => {
        let {orderId} = req.body;
        updateOrderStatus(orderId,2).then((result) => {
            res.json({code:200, data:result,msg:'接单成功'})
        })
    })
}

//骑手抢单
function robOrder(req, res, next) {
    guard(req,next, () => {
        let {orderId} = req.body;
        updateOrderStatus(orderId,3).then((result) => {
            res.json({code:200, data:result,msg:'骑手接单成功'})
        })
    })
}

//骑手取餐
function getOrderFood(req, res, next) {
    guard(req,next, () => {
        let {orderId} = req.body;
        updateOrderStatus(orderId,4).then((result) => {
            res.json({code:200, data:result,msg:'骑手取餐成功'})
        })
    })
}

//骑手确认完成配送
function finishOrder(req, res, next) {
    guard(req,next, () => {
        let {orderId} = req.body;
        updateOrderStatus(orderId,5).then((result) => {
            res.json({code:200, data:result,msg:'完成配送'})
        })
    })
}




/**
 * 
 * @param {订单id} orderId 
 * @param {要修改的状态值} nextStatus 
 */
function updateOrderStatus(orderId,nextStatus) {
    let sql = `UPDATE myDB.Orders SET status = ${nextStatus} WHERE (orderId = ${orderId})` 
    return querySql(sql)
}




module.exports = {
  createOrder,
  OrderInfo,
  OrderPayStatus,
  confirmOrder,
  robOrder,
  getOrderFood,
  finishOrder
}